//TokenCenter.js
// 智能合约地址
import Web3 from "web3";
import {MSGSENDER} from "./TokenCenter"
import {ERC20ABI} from "./contractABI"
// 智能合约ABI
const web3 = new Web3(Web3.givenProvider);
let contract;
import "nprogress/nprogress.css"
import TimeStatus from "./Time"
import { Message,Notification } from 'element-ui';
import { start, done, configure } from "nprogress"

/**
 * 初始化
 * @param option
 */
async function  Init(option){
    contract = new web3.eth.Contract(ERC20ABI, option);
    return true
}

/**
 * 授权交易
 * @param option Token Address
 * @param extra  spender  Exchange Address    amount 交易值
 * @param callback  返回值
 */
function tokenApprove(option,extra,callback){
    Init(option).then(r=>{
        if(r){
            contract.methods.approve(extra.spender,extra.amount).send({from:MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}

function getTotalResidualBurn(option,callback){
    Init(option).then(r=>{
        if(r){
            contract.methods.balanceOf(MSGSENDER).call()
                .then(r=>callback(r))
        }
    })
}
export default {
    tokenApprove,
    getTotalResidualBurn
}
